package com.uu.safeguard.controller;

import com.uu.safeguard.result.Result;
import com.uu.safeguard.result.ResultFactory;
import com.uu.safeguard.utils.CommonUtil;
import com.uu.safeguard.utils.DbUtil;
import com.uu.safeguard.utils.JedisUtil;
import com.uu.safeguard.utils.JwtUtil;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("powerValueLog")
public class PowerValueLogController {

    //正在充电功率曲线
    @PostMapping("chargingList")
    public Result chargingList(@RequestHeader("token") String token, String socket_sn,Integer charge_log_id){
        String memberSn = JwtUtil.getUserId(token);

        if(socket_sn  == null || socket_sn.equals("")){
            return ResultFactory.buildFailResult("参数有误");
        }

        Jedis jedis = JedisUtil.getJedis();
        jedis.select(1);
        if(!jedis.exists(socket_sn)){
            JedisUtil.close(jedis);
            return ResultFactory.buildFailResult("暂无数据");
        }
        List<String> list = jedis.lrange( socket_sn, 0, -1 );
        JedisUtil.close(jedis);

        if(list.size() == 0){
            return ResultFactory.buildFailResult("暂无数据");
        }

        Collections.reverse(list); //list反序

        List times = new ArrayList();
        List values = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonObject = JSONObject.fromObject(list.get(i));
            if(charge_log_id  != null && charge_log_id > 0 && !jsonObject.get("charge_log_id").toString().equals(charge_log_id + "")){
                continue;
            }
            String time = jsonObject.get("power_time").toString();
            String value = jsonObject.get("power_value").toString();
            times.add(CommonUtil.stampToDate(Long.parseLong(time),"HH:mm"));
            values.add(value);
        }

        JSONObject resultData = new JSONObject();
        resultData.put("times",times);
        resultData.put("values",values);
        return ResultFactory.buildSuccessResult(resultData);
    }

    //充电记录功率曲线
    @PostMapping("chargedList")
    public Result chargedList(@RequestHeader("token") String token,Integer charge_log_id){
        String memberSn = JwtUtil.getUserId(token);

        if(charge_log_id  == null || charge_log_id < 1){
            return ResultFactory.buildFailResult("参数有误");
        }

        DbUtil db = new DbUtil();
        List where  = new ArrayList(){{
            add(new ArrayList<Object>(){{
                add("charge_log_id");
                add("=");
                add(charge_log_id);
            }});
        }};
        List list = db.table("power_value_log").where(where).order("power_time asc").limit(9999999).select("*");

        if(list.size() == 0){
            return ResultFactory.buildFailResult("暂无数据");
        }

        List times = new ArrayList();
        List values = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonObject = JSONObject.fromObject(list.get(i));
            String time = jsonObject.get("power_time").toString();
            String value = jsonObject.get("power_value").toString();
            times.add(CommonUtil.stampToDate(Long.parseLong(time),"HH:mm"));
            values.add(value);
        }

        JSONObject resultData = new JSONObject();
        resultData.put("times",times);
        resultData.put("values",values);
        return ResultFactory.buildSuccessResult(resultData);

    }
}
